package com.zxn.ssyx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxn.ssyx.common.result.Result;
import com.zxn.ssyx.service.RoleService;
import com.zxn.ssyx.vo.acl.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zxn.ssyx.model.acl.*;

import java.util.List;

@RestController
@RequestMapping("/admin/acl/role")
@Api(tags = "角色管理")
@Slf4j
@CrossOrigin  // 跨域
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "获取角色分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(@ApiParam(name = "page",value = "当前页码",required = true) @PathVariable Long page,
                        @ApiParam(name = "limit",value = "每页记录数",required = true) @PathVariable Long limit,
                        @ApiParam(name = "roleQueryVo",value = "查询对象",required = true) RoleQueryVo roleQueryVo){
        Page<Role> pageParam = new Page<>(page,limit);
        IPage<Role> rolePage = roleService.index(pageParam,roleQueryVo);
        return Result.ok(rolePage);
    }

    @ApiOperation(value = "根据id查询角色")
    @GetMapping("/get/{id}")
    public Result get(@ApiParam(name = "id",value = "角色id") @PathVariable Long id){
        Role roleById = roleService.getById(id);
        return Result.ok(roleById);
    }

    @ApiOperation(value = "新增角色")
    @PostMapping("/save")
    public Result save(@ApiParam(name = "role",value = "新增角色信息") @RequestBody Role role){
        boolean save = roleService.save(role);
        if (save){
            return Result.ok("保存角色成功！！");
        }
        return Result.fail("保存角色失败！！");
    }

    @ApiOperation(value = "修改角色")
    @PutMapping("/update")
    public Result updateById(@RequestBody Role role) {
        boolean result = roleService.updateById(role);
        if (result){
            return Result.ok("修改角色成功！！");
        }
        return Result.fail("修改角色失败！！");
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean result = roleService.removeById(id);
        if (result){
            return Result.ok("删除角色成功！！");
        }
        return Result.fail("删除角色失败！！");
    }

    @ApiOperation(value = "根据id列表删除角色")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean result = roleService.removeByIds(idList);
        if (result){
            return Result.ok("根据id删除角色成功！！");
        }
        return Result.fail("根据id删除角色失败！！");
    }
}
